'''
@File    :   SessionControl_0x10_test.py
@Time    :   2024/08/27 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

from pytest_assume.plugin import assume

import pytest
import logging
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

'''会话切换'''
SessionControl_Default = [  ([0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x81], []),
                            ([0x10, 0x82], []),
                            ([0x10, 0x83], []),
                        ]

SessionControl_Boot = [     ([0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x82], []),
                            ([0x10, 0x81], []),
                        ]

SessionControl_Extended = [ ([0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x00, 0xC8]),
                            ([0x10, 0x81], []),
                            ([0x10, 0x83], []),
                        ]

'''NRC7E'''
SessionControl_Boot_NRC7E = [   ([0x10, 0x03], [0x7F, 0x10, 0x7E]),
                                ([0x10, 0x83], [0x7F, 0x10, 0x7E]),
                            ]

SessionControl_Extended_NRC7E = [   ([0x10, 0x02], [0x7F, 0x10, 0x7E]),
                                    ([0x10, 0x82], []),
                            ]

'''NRC12'''
SessionControl_NRC12 = [    ([0x10, 0x04], [0x7F, 0x10, 0x12]),
                            ([0x10, 0x05], [0x7F, 0x10, 0x12]),
                            ([0x10, 0x06], [0x7F, 0x10, 0x12]),
                            ([0x10, 0x84], [0x7F, 0x10, 0x12]),
                            ([0x10, 0x85], [0x7F, 0x10, 0x12]),
                            ([0x10, 0x86], [0x7F, 0x10, 0x12]),
                        ]
'''NRC13'''
SessionControl_NRC13 = [    ([0x10], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x01, 0x02], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x02, 0x03], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x03, 0x02], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x01, 0xFF], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x02, 0xFF], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x03, 0x10], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x05, 0x06], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x81, 0xFF], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x82, 0xFF], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x83, 0x10], [0x7F, 0x10, 0x13]),
                            ([0x10, 0x85, 0x06], [0x7F, 0x10, 0x13]),
                        ]

'''NRC11'''
SessionControl_NRC11 = [    ([0x23], [0x7F, 0x23, 0x11]),
                            ([0x2A], [0x7F, 0x2A, 0x11]),
                            ([0x2C], [0x7F, 0x2C, 0x11]),
                            ([0x2F], [0x7F, 0x2F, 0x11]),
                        ]

class Test_SessionControl_Phy_Boot(object):
    '''
    test SessionControl with physical address in programming mode
    '''
    @allure.severity('normal')
    @allure.story('Session Control Test in BL')
    @allure.description('Test session change from 0x10 01->-0x10 03>0x10 02->0x10 02->0x10 03')
    def test_sessionSwitch1_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x7F, 0x10, 0x7E], 0, iStep, False)



    @allure.severity('normal')
    @allure.description('Test session change from 0x10 02->0x10 02->0x11 01')
    def test_sessionSwitch2_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [0x51, 0x01], 0, iStep, False)
        time.sleep(2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27, 0x01], [0x7F, 0x27, 0x7F], 0, iStep, False)

    @allure.severity('normal')
    @allure.story('Session Control Test in BL')
    @allure.description('Test session change from 0x10 01->0x10 02->0x10 03->0x10 01->0x10 02->0x10 01')
    def test_sessionSwitch4_Phy(self, action, Driver):
        iStep = 1
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x7F, 0x10, 0x7E], 0, iStep, False)
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False)
        time.sleep(6)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False)



    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 02->0x11 03')
    def test_sessionSwitch3_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False)
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x03], [0x51, 0x03], 0, iStep, False)


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_Boot]
    @allure.severity('normal')
    @allure.description("Test positive response of SID 0x10 in bootloader")
    @pytest.mark.parametrize('req,resp', SessionControl_Boot, ids=ids)
    def test_sessionControl_Boot_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_NRC11]
    @allure.severity('minor')
    @allure.story('Test NRC11 of SID 0x10 in bootloader with physical addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_NRC11, ids=ids)
    def test_sessionControl_NRC11_Boot_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_NRC12]
    @allure.severity('minor')
    @allure.story('Test NRC12 of SID 0x10 in bootloader with physical addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_NRC12, ids=ids)
    def test_sessionControl_NRC12_Boot_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_NRC13]
    @allure.severity('minor')
    @allure.story('Test NRC13 of SID 0x10 in bootloader with physical addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_NRC13, ids=ids)
    def test_sessionControl_NRC13_Boot_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_Boot_NRC7E]
    @allure.severity('minor')
    @allure.story('Test NRC7E of SID 0x10 in bootloader with physical addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_Boot_NRC7E, ids=ids)
    def test_sessionControl_NRC7E_Boot_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)


class Test_SessionControl_Fun_Boot(object):
    '''
    test SessionControl with functional address in programming mode
    '''
    @allure.severity('normal')
    @allure.story('Session Control Test in BL')
    @allure.description('Test session change from 0x10 01->0x10 02->0x10 02->0x10 01')
    def test_sessionSwitch1_Fun(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False, 'UDSFun')
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False, 'UDSFun')



    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 02->0x11 01')
    def test_sessionSwitch2_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False, 'UDSFun')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x01], [0x51, 0x01], 0, iStep, False, 'UDSFun')
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27, 0x01], [], 0, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.story('Session Control Test in BL')
    @allure.description('Test session change from 0x10 01->0x10 02->0x10 02->0x10 01->0x10 03->0x10 02->0x10 03')
    def test_sessionSwitch4_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 2, iStep, False, 'UDSFun')
        time.sleep(0.2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x01], [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False, 'UDSFun')
        time.sleep(0.5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False, 'UDSFun')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x31, 0x01, 0x02, 0x03], [0x71, 0x01], 0, iStep, False, 'UDS')
        time.sleep(0.2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False, 'UDSFun')
        time.sleep(6)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False, 'UDSFun')



    @allure.severity('normal')
    @allure.description('Test session change from 0x10 01->0x10 03->0x10 02->0x11 03')
    def test_sessionSwitch3_Fun(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x31, 0x01, 0x02, 0x03], [0x71, 0x01], 3, iStep, False, 'UDS')
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x02], [0x50, 0x02, 0x00, 0x32, 0x00, 0xC8], 0, iStep, False, 'UDSFun')
        time.sleep(1)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x11, 0x03], [0x51, 0x03], 0, iStep, False, 'UDSFun')


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_Boot]
    @allure.severity('normal')
    @allure.description("Test positive response of SID 0x10 in bootloader")
    @pytest.mark.parametrize('req,resp', SessionControl_Boot, ids=ids)
    def test_sessionControl_Boot_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun')


    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_NRC11]
    @allure.severity('minor')
    @allure.story('Test NRC11 of SID 0x10 in bootloader with functional addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_NRC11, ids=ids)
    def test_sessionControl_NRC11_Boot_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, False, 'UDSFun')

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_NRC12]
    @allure.severity('minor')
    @allure.story('Test NRC12 of SID 0x10 in bootloader with functional addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_NRC12, ids=ids)
    def test_sessionControl_NRC12_Boot_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, False, 'UDSFun')

    
    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_NRC13]
    @allure.severity('minor')
    @allure.story('Test NRC13 of SID 0x10 in bootloader with functional addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_NRC13, ids=ids)
    def test_sessionControl_NRC13_Boot_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDSFun')

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SessionControl_Boot_NRC7E]
    @allure.severity('minor')
    @allure.story('Test NRC7E of SID 0x10 in bootloader with functional addressing')
    @pytest.mark.parametrize('req,resp', SessionControl_Boot_NRC7E, ids=ids)
    def test_sessionControl_NRC7E_Boot_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, False, 'UDSFun')